<!--多图片上传组件,直接以base64格式保存在前端-->
<template>
  <a-form-item :label="label">
    <a-input
        v-show="false"
        v-decorator="vDecorator"
    ></a-input>
    <AntPicUpload
        :style="getStyle"
        ref="picUpload"
        :uploadText="uploadText"
        :showMax="showMax"
        :max="max"
        :width="percentageLayoutWidth"
        :disabled="isDisabled"
        @updateFileList="updateFileList"
    ></AntPicUpload>
  </a-form-item>
</template>

<script>
import './css/form.less'
import { mixin } from './js/form.js'

export default {
  name: 'AntFormPicUpload',
  mixins: [mixin],
  props: {
    /**
     * 是否禁用
     */
    disabled: {
      type: Boolean,
      default: false
    },
    /**
     * 是否由form表单自动布局
     */
    autoLayoutByForm: {
      type: Boolean,
      default: false
    },
    /**
     * 显示文本
     */
    label: {
      type: String,
      default: ''
    },
    /**
     * 输入提示
     */
    placeholder: {
      type: String,
      default: '请上传图片'
    },
    /**
     * 绑定字段 ['phone' ,{ rules: [{ required: true},{validator:function(rule, value, callback)] }],自定义验证方法时callback回调函数必须在方法中调用，
     * 失败返回错误提示callback("xxx")，成功返回callback()
     * 字段值类似 [{"uid": "2504210914160000011","name": "2504210914160000011","url": "",}]
     */
    vDecorator: {
      type: Array,
      default: () => []
    },
    /**
     * 上传按钮显示的文字
     */
    uploadText: {
      type: String,
      default: '上传图片'
    },
    /**
     * 是否显示最大上传数量
     */
    showMax: {
      type: Boolean,
      default: true
    },
    /**
     * 最大上传数量，默认1张图片
     */
    max: {
      type: Number,
      default: 1
    },
    /**
     * css样式，优先级最高
     */
    cstyle: {
      type: String,
      default: ''
    },
    /**
     * 非自动布局的宽度，优先级最低
     */
    defaultWidth: {
      type: Number,
      default: 100
    }
  },
  data() {
    return {
      //是否禁用和输入提示，由AntForm组件调用
      cdisabled: false,
      cplaceholder: this.placeholder ? this.placeholder : '请' + this.label
    }
  },
  computed: {
    /**
     * 获取样式
     */
    getStyle() {
      return { width: (this.percentageLayoutWidth ? this.percentageLayoutWidth : this.defaultWidth) + 'px', ...this.cstyle }
    }
  },
  mounted() {
    // 自动添加验证为空时的message
    let vDecorator = this.vDecorator
    if (vDecorator.length > 1 && vDecorator[1].rules) {
      let rules = vDecorator[1].rules
      rules.forEach(obj => {
        if (obj.required && !obj.message) {
          obj.message = '请上传' + this.label
        }
      })
    }
  },
  methods: {
    /**
     * 更新表单值
     * @param fileList
     */
    updateFileList(fileList) {
      const obj = {}
      const key = this.vDecorator[0]
      obj[key] = fileList && fileList.length > 0 ? JSON.stringify(fileList) : null
      // 重新赋值
      const c = this.getParentForm(this.$parent)
      c.setFieldsValue(obj)
    },
    /**
     * 获取最顶层form表单
     * @param param
     * @returns {*}
     */
    getParentForm(param) {
      if (param.$refs.aform) {
        return param.$refs.aform.form
      } else {
        return this.getParentForm(param.$parent)
      }
    },
    /**
     * 赋值之前的转换操作,由AntForm组件自动调用
     * 参数：recordPara，转换之前的record
     * 返回值：转换之后的可用于赋值的对象
     */
    beforeSetValue(recordPara) {
      const record = {}
      let key = this.vDecorator[0]
      let val = recordPara[key]
      let fileListStr = null
      if (val) {
        // 图片组件赋值
        this.$refs.picUpload.setFileList(val)
        fileListStr = JSON.stringify(val)
      }
      record[key] = fileListStr
      return record
    },
    /**
     * 取值之前的转换操作,由AntForm组件自动调用
     * 参数：recordPara，转换之前的record
     * 返回值：转换之后的对象
     */
    beforeGetValue(recordPara) {
      let record = {}
      let key = this.vDecorator[0]
      record[key] = this.$refs.picUpload.getFileList()
      return record
    }
  }
}
</script>

<style lang="less" scoped>

</style>
